﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace AlphaSolutionsGP
{
    public class Course
    {
        #region properties
        MySqlConnection connection = new MySqlConnection("server=194.81.104.22;" + "User Id=alpha;" + "database=alpha_db;" + "password=bravo;"); 
        private string _courseCode;
        private string _title;
        private string _department;
        private int _year;
        private int _maxStudents;
        private string _description;
        private string _requirements;
        #endregion
        #region constructors
        public Course(string coursecode, string title, string department, int year, int maxstudents, string description, string requirements)
        {
            this._courseCode = coursecode;
            this._title = title;
            this._department = department;
            this._year = year;
            this._maxStudents = maxstudents;
            this._description = description;
            this._requirements = requirements;
        }
        public Course()
        { }
        #endregion
        public void createCourse()
        {
            try
            {
                connection.Open();
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Connection failed");
            }
            try
            {
                MySqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "insert into COURSE values('" + this._courseCode + "','" + this._title + "','" + this._year + "','" + this._department + "','" + this._maxStudents + "','" + this._description + "','" + this._requirements + "');";
                MySqlDataReader run = cmd.ExecuteReader();
                MessageBox.Show("Course- " + this._courseCode + " " + this._title + " was created successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Unable to execute command,\n\n" + ex.ToString());
            }
            finally
            {
                connection.Close();
            }
        }
        public void ammendCourse()
        {
            try
            {
                connection.Open();
                MySqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "UPDATE COURSE SET title='" + this._title + "', year='" + this._year + "', department='" + this._department + "', max_students='" + this._maxStudents + "', description='" + this._description + "', requirements='" + this._requirements + "' WHERE course_Code='" + this._courseCode + "'";
                MySqlDataReader run = cmd.ExecuteReader();
                MessageBox.Show("Course- " + this._courseCode + " " + this._title + " was updated successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Connection failed");
            }
            finally
            {
                connection.Close();
            }
        }
        public void deleteCourse()
        {
            try
            {
                connection.Open();
                try
                {
                    MySqlCommand cmd = connection.CreateCommand();
                    cmd.CommandText = "delete from COURSE where course_Code='" + this._courseCode + "'";
                    MySqlDataReader run = cmd.ExecuteReader();
                    MessageBox.Show("Course- " + this._courseCode + " " + this._title + " has been deleted successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    run.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to delete the course as there are students assigned to it.");
                }

                
            }
            catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Connection failed\n\n" + ex.ToString());
            }
            finally
            {
                
                connection.Close();
            }
        }

        #region getters and setters
        public int MaxStudents
        {
            get { return _maxStudents; }
            set { _maxStudents = value; }
        }
        

        public int Year
        {
            get { return _year; }
            set { _year = value; }
        }
        

        public string Department
        {
            get { return _department; }
            set { _department = value; }
        }
        

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
        
        public string CourseCode
        {
            get { return _courseCode; }
            set { _courseCode = value; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public string Requirements
        {
            get { return _requirements; }
            set { _requirements = value; }
        }
        #endregion
    }
}
